package function.listen;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.HashMap;
import java.util.Set;

import javax.swing.BorderFactory;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import frame.master;
import frame.data.contrldata;

public class textarea1listen {
	public textarea1listen() {
		contrldata.jt=new JTextArea();
		contrldata.jt.setFont(new Font(master.set.getNowziti(),master.set.getPlain(),master.set.getFontsize()));
		contrldata.js=new JScrollPane(contrldata.jt);
		contrldata.js.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		contrldata.js.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		contrldata.js.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 0, new Color(188,188,188)));
	}
	public void loadtextarealisten(master ma) {
		contrldata.jt.addKeyListener(new KeyListener() {
			private boolean isdown=false;
			private boolean isctrl=false;
			@Override
			public void keyTyped(KeyEvent e) {}
			@Override
			public void keyReleased(KeyEvent e) {
				if(isctrl&&e.getKeyCode()==78) {
					master.m1l.createnewfile(ma);
					isctrl=false;
				}else {
					isdown=false;
					updatetexttofileinfo();
					if((e.getKeyCode()<37 || e.getKeyCode()>40)&& !isdown)master.t2l.updatelinenum(ma);
					master.fl.setfileisupdateofnum(master.editfilenum);
				}
			}
			@Override
			public void keyPressed(KeyEvent e) {
				if(!isctrl&&e.getKeyCode()==17) {isctrl=true;}else {
					isdown=true;
					if(e.getKeyCode()<37 || e.getKeyCode()>40)
						master.t2l.updatelinenum(ma);
				}
				contrldata.fileinfo.updateUI();
			}
		});
		contrldata.jt.addMouseMotionListener(new MouseMotionListener() {
			@Override
			public void mouseMoved(MouseEvent e) {}
			@Override
			public void mouseDragged(MouseEvent e) {contrldata.fileinfo.updateUI();}
		});
		contrldata.jt.addMouseListener(new MouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {}
			@Override
			public void mousePressed(MouseEvent e) {}
			@Override
			public void mouseExited(MouseEvent e) {}
			@Override
			public void mouseEntered(MouseEvent e) {}
			@Override
			public void mouseClicked(MouseEvent e) {contrldata.fileinfo.updateUI();}
		});
	}
	/**
	 * 更新编辑框编码
	 */
	public void updateteatareacode() {
		try {
			HashMap<Integer, String> a=master.fl.getfileinfoofnum(master.editfilenum).getContent();
			Set<Integer> b=a.keySet();
			contrldata.jt.setText("");
			String code=master.fl.getfileinfoofnum(master.editfilenum).getCode();
			if(code.equals("Old Code"))
				code=master.fl.getfileinfoofnum(master.editfilenum).getOldcode();
			for(int i:b) 
				contrldata.jt.append(new String(a.get(i).getBytes(),code)+((i==b.size()-1)?"":"\n"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 将编辑框的内容存入fileinfo
	 */
	public void updatetexttofileinfo() {
		String[] a=contrldata.jt.getText().split("\n");
		HashMap<Integer, String> content=new HashMap<>();
		for(int i=0;i<a.length;i++)content.put(i, a[i]);
		master.fl.getfileinfoofnum(master.editfilenum).setContent(content);
	}
	/**
	 * 加载文本到编辑框
	 */
	public void addtexttoarea(master ma) {
		HashMap<Integer, String> q=master.fl.getfileinfoofnum(master.editfilenum).getContent();
		contrldata.jt.setText("");
		Set<Integer> w=q.keySet();
		for(int i:w)contrldata.jt.append(q.get(i)+((i==q.size()+1)?"":"\n"));
		master.t2l.updatelinenum(ma);
	}
}
